<%@ LANGUAGE="VBSCRIPT" %>
<%'  ------------------------------------------------------------
  '  mz_advqspec.asp 
  '  ------------------------------------------------------------
  '  VCS INFO:
  '  
  '  $Revision:   1.20  $
  '  $Date:   10 May 1999 11:44:28  $
  '  $Author:   shsieh  $
  '  $Workfile:   mz_advqspec.asp  $
  '  ------------------------------------------------------------
  '  All Rights Reserved.  Copyright (c) 1988-1998 FileNET Corp.
  '  ------------------------------------------------------------
  '%>
<!-- #include file="islogon.asp" -->
<!-- #include file="JScriptFix.asp" -->
<%
    DebugFlag = 0
    RequestMethod = Request.ServerVariables("REQUEST_METHOD")
    if RequestMethod = "POST" then
        DocClass = Request.Form("doctype")
    else
        DocClass = Request.QueryString("doctype")
    end if 
    if Len(DocClass) = 0 then
        DocClass="GeneralProperties"
    end if

'Panagon IDM Web application uses the Session variable LibraryLogonID to keep track of
'whether the user has logged on.
'Please see logoncall.asp to see how the Session variable LibraryLogonID is set.
    LibraryLogonID = Session("LibraryLogonID")
    set LibraryObj = Server.CreateObject("IDMObjects.Library")
    LibraryObj.LogonID = LibraryLogonID
    
    On Error Resume Next
    set DocTypes = LibraryObj.FilterClassDescriptions( Application("idmObjTypeDocument"))
    if Len(Err.Description) then
        Response.Expires = 0
        Response.Status = "404 Not Found"
        Response.ContentType = "text/plain"
        Response.Write "Object not found: "
        Response.Write Err.Description
        Response.End
    end if

    set DocType = DocTypes(1) ' default
    ' determine which is the selected DocTYpe
    for i = 1 to DocTypes.Count
        if DocTypes(i).Label = DocClass then
            set DocType = DocTypes(i)
            exit for
        end if
    next
    set PropDescs = DocType.PropertyDescriptions 
%>


<% if DebugFlag then %>
Form<br>
<% For Each x In Request.QueryString %>
<%if not (Request.QueryString(x) = "") then %>
<!-- <INPUT TYPE=hidden name="<%= x %>" value="<%= Request.Form(x) %>"> -->
name="<%= x %>" value="<%= Request.QueryString(x) %>"<br>
<% end if %>
<% Next 
end if %>

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>

<head>
<meta http-equiv="Content-Type"
content="text/html">
<meta name="GENERATOR"
content="Microsoft FrontPage (Visual InterDev Edition) 2.0">
<title></title>
</head>

<body bgcolor="#FFFFFF">

<form name="search" action="mz_advqresult.asp" method="POST" target="QueryResult">

<B><FONT>Search Library '<%= Server.HTMLEncode(LibraryObj.Label) %>' </FONT></B> based on doctype 
    <select name="doctype" onChange="javascript:AdvancedSearch()">
            <option value="">&lt;Choose a type&gt;</option>
        <%for each documentClass in DocTypes%>
            <option value="<%=Server.URLEncode(documentClass.Name)%>" <%if documentClass.Name=DocClass then %> selected <% end if%>>
            <%=Server.HTMLEncode(documentClass.Name)%></option>
        <%next%>
    </select>
  
    <% IEBrowser = InStr(Request.ServerVariables("HTTP_USER_AGENT"), "MSIE")
if IEBrowser then %>
<table border="1" width="100%" cellpadding="0" cellspacing="1">
<% else %>
<table border="0" width="100%" cellpadding="0" cellspacing="1">
<% end if %>
    <tr>
    <td align="left" width="30%" bgcolor="#EECCFF"><font size="3"><strong>Property</strong></font></td>
    <td align="left" width="15%" bgcolor="#EECCFF"><font size="3"><strong>Operator</strong></font></td>
    <td align="left" width="40%" bgcolor="#EECCFF"><font size="3"><strong>Value</strong></font></td>
    <td align="left" width="15%" bgcolor="#EECCFF"><font size="3"><strong>&nbsp</strong></font></td>
    </tr>

    <%
    ' allow the user to enter 5 search criteria
    ' each criteria can be selected from a dropdown list of properties of this DocType 
    for i = 0 to 4
    %>
    <tr>

    <td align=left><em> <select name="PROP_<%= i %>" size="1" onChange="javascript:SetPropType(this,<%=i%>)">
            <option>
        <% 
        ' enumerate through properties of this DocTYpe, and create the properties selection list
        for k = 1 to PropDescs.Count
            set PropDesc = PropDescs(k)
            PropTypeStr = " " ' PropType needs to be known for client side validation and valid Operators
            if PropDesc.TypeID = Application("idmTypeDate") then
                PropTypeStr="date"
            elseif PropDesc.GetState(Application("idmPropMustSetByID")) then
                PropTypeStr="byId"
            elseif PropDesc.TypeID = Application("idmTypeLong") or PropDesc.TypeID = Application("idmTypeShort") _ 
             or PropDesc.TypeID = Application("idmTypeSingle") _
             or PropDesc.TypeID = Application("idmTypeUnsignedShort") or PropDesc.TypeID = Application("idmTypeUnsignedLong") then
                        PropTypeStr="num"
            elseif PropDesc.Label = "Comment" then
                PropTypeStr="comment"
            else PropTypeStr="string"
            end if
         %>
            <option value="<%= PropDesc.Name %>,<%=PropTypeStr%>" > <%= PropDesc.Label %>&nbsp;&nbsp;&nbsp;&nbsp;</option>
        <% next %>
            </select>

    <td align=left><em><select name="OPER_<%= i %>" size="1" onChange="javascript:checkOperator(this, <%=i%>)">
            <option value="" selected >
            <option value="=" >is equal
            <option value="<>" >not equal
            <option value="<" >is less than
            <option value=">" >is greater than
            <option value=">=" >greater equal
            <option value="<=" >less equal
            <option value=" LIKE">like
            <option value=" NOT LIKE">not like
        </select>
        </em></td>
    <td align=left><em>
        <input type="text" size="40" name="VALUE_<%= i %>" value=""  onChange="CheckValue(this, <%=i%>)">   </em></td>
    <td align=left><em> 
    <% if i < 4 then %> <select name="BOOL_<%=i%>">
        <option value="AND" selected>and
        <option value="OR">or
        </select>
        &nbsp;&nbsp;&nbsp;&nbsp;</em> 
    <% else %>
        <input type="hidden" name="BOOL_<%=i%>" value="" >
    <% end if %> </td>
    <input type="hidden" name="TYPE_<%=i%>" value="" >
    <input type="hidden" name="NAME_<%=i%>" value="xx" >
    </tr>
    <% next %>

    </table>
    <table width="100%">
    <tr>
    <td width="30%"><font color="#000080"><em><strong>Document Contains: </strong></em></font> </td>
    <td> <input type="text" size="64" name="DocContent"></td>
    </tr>
    <tr>
    <td width="30%"><font color="#000080"><em><strong>Sort Order : </strong></em></font> </td>
    <td> <select name="SortBy">
        <option value="idmId" selected>Item ID
        <option value="idmName">Title
        <option value="idmDateAdded">Date Added
        </select>
    </td>
    </tr>
    </table>

    <input type="hidden" name="NewSearch" value="1">
    <input type="hidden" name="HiddenQueryCondition" value="">
    <input type="hidden" name="PropDescCount" value="<%=PropDescs.Count%>">


    <a href="javascript:Submit()"><img alt="Search" src="images/sbutt.gif" align=bottom border=0 width=43 height=43></a><em><strong>Search</strong></em>
    <!-- or use the following
    <input type="submit" name="B1" value="  Search  ">
    -->
</form>
</body>
</html>

<!-- #include file="checkutil.asp" -->

<SCRIPT LANGUAGE=JavaScript>
<!--
// selection of a different DocType displays the Advanced Search Page w/ 
// a different properties selection list
function AdvancedSearch()
{
    docTypeStr = document.search.doctype.options[document.search.doctype.selectedIndex].value;
    parent.QueryResult.location.href = "blank.htm";
    location.href = "mz_advqspec.asp?doctype=" + docTypeStr;

}

// validates inputed data depending on the property data type
function CheckValue(inp, i) {
    var PropTypeStr = document.search.elements[i*6+5].value;
    // check if there is a Property selected
    if (PropTypeStr) {
        if (PropTypeStr == "date") {
            return CheckDate(inp);
        }
        else if ( PropTypeStr == "num") {
            return CheckInteger(inp); 
        }
        else
            return true;
    }
    else {
        document.search.elements[i*6+1].focus();
        alert ("You have to select a property before entering a value.");
        return false;
    }
}

// parse the selected value and set the hidden fields Property Type / Name
function SetPropType(inp, i) {
    var pos1 = 0;
    var propName = "";
    var propType = "";
    var propStr = "";
    propStr = document.search.elements[i*6+1].options[document.search.elements[i*6+1].selectedIndex].value;
    // alert(propStr);
    pos1 = propStr.indexOf(",");
    if (pos1 > 0) {
        propName = propStr.substring(0, pos1);
        propType = propStr.substring(pos1+1, propStr.length);
    }
    
    document.search.elements[i*6+5].value = propType;
    document.search.elements[i*6+6].value = propName;

}

// check validity of selected operator depending on the Property type
function checkOperator(inp, i) {
    
    var PropTypeStr = document.search.elements[i*6+5].value;
    var value = document.search.elements[i*6+2].options[document.search.elements[i*6+2].selectedIndex].value;
    
    // alert(PropTypeStr);
    // alert(value);
    if (PropTypeStr) {
     if (PropTypeStr == "date") {
        if ( value == ">" || value == "<")
            return true;
        else {
            inp.selectedIndex = 0;
            alert ("Date types accept only 'is greater than','is less than' operators.");
            return false;
        }
     }
     else if (PropTypeStr == "byId") {
        if (value =="=" || value == "!=" )
            return true;
        else {
            inp.selectedIndex = 0;
            alert ("This property accepts only 'is equal','not equal' operators.");
            return false;
        }
     }
     else if (PropTypeStr =="comment") {
        if (value ==" LIKE" ||  value ==" NOT LIKE")
            return true;
        else {
            inp.selectedIndex = 0;
            alert ("This property accepts only 'like','not like' operators.");
            return false;
        }
     }
     else if ( PropTypeStr == "num") { 
        if ( value == " LIKE" || value == " NOT LIKE") {
            inp.selectedIndex = 0;
            alert ("This property does not accept 'like','not like' operators.");
            return false;
        }
     }
    }
    else {
        document.search.elements[i*6+1].focus();
        alert ("You have to select a property before selecting the operator.");
        return false;
    }
    return true;
}

function substitute(string, match, replacement) {
  // Within STRING, replace any MATCHing string with the REPLACEMENT.
  var result = '';
  var index = 0;
  var lastIndex = index;
  while (string.length > lastIndex) {
    index = string.indexOf(match, lastIndex);
    if (index == -1) { break }
    result += string.substring(lastIndex, index) + replacement;
    lastIndex = index + match.length;
  }
  result += string.substring(lastIndex, string.length);
  return result;
}

// check the inputed search criteria
// build the SQL statement
// call the mz_advqresult.asp to execute the search
function Submit()
{
    var prev = 0;
    var value = " ";
    var boolStr = " AND ";
    var operStr = " ";
    var propValue = " ";
    var propName = " ";
    // for (var i=0; i < document.search.PropDescCount.value; i++) {
    for (var i=0; i <= 4; i++) {
        // check if there is a value entered for this control; there are 6 inputs/property
        propValue = document.search.elements[i*6 + 3].value;
        propName = document.search.elements[i*6 + 6].value;
        
        if (propValue == "") break;
        else  {
            operStr = document.search.elements[i*6+2].options[document.search.elements[i*6+2].selectedIndex].value;
            
            if (operStr != "") {
                if (prev)
                    value = value + boolStr; // concat the boolean of the last property
                else 
                    value = "WHERE ";   // start building the where clause
                if (operStr == " LIKE" || operStr == " NOT LIKE") {
                    value = value + propName + operStr + " '" + substitute(propValue,"'","''") + "'";
                }
                else {
                    if (document.search.elements[i*6+5].value == "num")
                    value = value + propName + operStr + substitute(propValue,"'","''");    
                    else
                    value = value + propName + operStr + "'" + substitute(propValue,"'","''") + "'";
                }
            }
            else {
                document.search.elements[i*6+2].focus();
                alert ("You have to select an operator, line." + i);
                // return false;
            }
            // keep the bool of this input 
            if ( i < 4)
            {
                boolStr = " " + document.search.elements[i*6 + 4].options[document.search.elements[i*6+4].selectedIndex].value + " ";
            }   
            prev = 1;
        }
    
    }
    
    if (document.search.DocContent.value) {
        value = value + " CONTENTS HAS PHRASE = '" + substitute(document.search.DocContent.value,"'","''") + "'"; 
        prev = 1;
    }
    if (prev == 0) {
        alert ("There is no search criteria specified.");
        // return false;    
    }
    else {
        sortStr = document.search.SortBy.options[document.search.SortBy.selectedIndex].value; 
        value = value + " ORDER BY " + sortStr; // document.search.SortBy.options[document.search.SortBy.selectedIndex].value;
        document.search.HiddenQueryCondition.value = value;
        document.search.submit();
        // return true;
    }
}
//-->
</SCRIPT>